/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jsForms.Components.DOMConfig = {
	/*Init: function(){
	 return jsForms.Components.Component.Init.call(this);
	 },*/
	/*
	 this.P.ComponentReal = real;
	 this.P.ComponentDesign = design;
	 DOMInspector - список всех объектов
	 SelectDOMItem - выбраный дом элемент
	 */
	/*
	 Item:
		 isRoot,
		 isContent,
		 isText,
		 isComponent,
		 contentName,
		 elementR,
		 elementD,
		 componentR
	 */
	SetComponent: function(design,compType){
		this.P.ComponentDesign = design;
		var tree = this.C.tvComponentDOM;
		tree.Clear();
		this.C.TreeViewMenu.hide();
		if (this.P.DOMInspector) {
			delete (this.P.DOMInspector);
			delete (tree.P.DOMInspector);
		};
		if (!this.P.ComponentDesign) {
			return;
		}
		
		function addToParent(item){
			if (item.p != undefined) {
				var parentNode = jobList[item.p].nodeTV;
				if (parentNode) {
					parentNode.AddChild(nodeTV);
				}
			}
		}
		
		var obj = this.P.ComponentDesign;
		
		var jobList = [{
			node : design
		}];
		this.P.DOMInspector = jobList;
		tree.P.DOMInspector = jobList;
		var index = 0;
		
		function createContent(node){
			var listContent = jsForms.DesignMode.GetContents(node.Type);
			
			for (var i = 0; i < node.Children.length; i++) {
				var ii = node.Children[i];
				if (ii.isContent == true) {
					jobList.push({
						node: ii,
						p: index
					});
					if (ii.InTo == undefined)
					{
						ii.InTo ='content';
					}
					if (listContent[ii.InTo] != undefined) {
						delete (listContent[ii.InTo]);
					}
				}
			}
			
			for (var i in listContent) {
				var newContent = new jsForms.Node('&gt;content['+ i + ']');
				newContent.InTo = i;
				newContent.isContent = true;
				newContent.Tag = 'CONTENT';
				node.appendNode(newContent);
				jobList.push({
						node: newContent,
						p: index
					});
			}
		}
		
		while (index < jobList.length) {
			var nodeTV = tree.CreateNode(index);
			var item = jobList[index];
			var node = item.node;
			item.nodeTV = nodeTV;
			
			if (node.isComponent == true) {
				createContent(node);
			}
			else {
				for (var i = 0; i < node.Children.length; i++) {
					jobList.push({
						node: node.Children[i],
						p: index
					});
				}
			}
			//debugger;

			if (index == 0) {
				nodeTV.SetText('&lt;' + compType + '&gt;');
			}
			else {
				nodeTV.SetText(jsForms.DesignMode.GenerateNodeName(node));
			}
			addToParent(item);
			index++;
		}
		
		var root = jobList[0].nodeTV;
		root.show();
		tree.AddChild(root);
		this._ExecEvent('onSelect', undefined);
	},
	StartDrag_node:function()
	{
		jsForms.GetObject(this).hide();
	},
	StopDrag_node: function(e, ui){
		var tv = jsForms.GetObjectByType(this, 'TreeView');
		if (tv) {
			var node = jsForms.GetObject(this);
			var pNode = tv.GetParent(node);
			ui.helper.remove();
			var ap = ui.absolutePosition;
			var ee = document.elementFromPoint(ap.left, ap.top);
			//debugger;
			var nodeTo = jsForms.GetObjectByType(ee, 'TreeViewNode');
			if (nodeTo && pNode) {
				var id = node.GetId();
				var itemTarget = tv.P.DOMInspector[id];
				var id = nodeTo.GetId();
				var itemTo = tv.P.DOMInspector[id];
				if (jsForms.DesignMode.MoveElement(itemTarget.node, itemTo.node) == true) {
					pNode.RemoveChild(node);
					nodeTo.AddChild(node);
					//this._ExecEvent('onMove', itemTarget);
				}
			}
			
		}
		node.show();	
	},
	DownElement_Click: function(){
		var item = this.P.SelectDOMItem;
		if (!item) {
			return;
		}
		if(jsForms.DesignMode.MoveElementUpDown('down',item.node)== true)
		{
			var prev = item.nodeTV.next();
			prev.after(item.nodeTV);
			this.UpdateTreeViewMenu();
			this._ExecEvent('onMove', item);
		}
	},
	UpElement_Click: function(){
		var item = this.P.SelectDOMItem;
		if (!item) {
			return;
		}
		
		if (jsForms.DesignMode.MoveElementUpDown('up',item.node)== true)
		{
			var prev = item.nodeTV.prev();
			prev.before(item.nodeTV);
			this.UpdateTreeViewMenu();
			this._ExecEvent('onMove', item);
		}
	},
	DOM_onChangeSelect: function(node){
		this.C.TreeViewMenu.hide();
		if (!node) {
			return
		}
		if (this.P.OldSelect) {
			this.P.OldSelect.draggable("disable");
		}
		this.P.OldSelect = node;
		var item = this.P.DOMInspector[node.GetId()];
		this.P.SelectDOMItem = item;
		//debugger;
		var DOMNode = item.node;
		if (DOMNode.isRoot != true) {
			this.UpdateTreeViewMenu();
		}
		if (node && DOMNode.isRoot != true && DOMNode.isContent != true) {
			node.draggable({
				helper: "clone",
				opacity: 0.40,
				stop: this.StopDrag_node,
				start: this.StartDrag_node
			}).draggable("enable");
		}
		
		if (!item) {
			return;
		}
		this._ExecEvent('onSelect', item);
	},
	UpdateTreeViewMenu:function()
	{
		var node = this.P.OldSelect;
		var off = node.offset();
		var offtv = this.C.tvComponentDOM.offset();
		this.C.TreeViewMenu.css({left:off.left-offtv.left+node.C.text.width()+'px',top:off.top-offtv.top+'px'}).show();
	},
	Insert_Element:function()
	{
		var node = this.P.OldSelect;
		if (!node) {
			return;
		}
		var id = node.GetId();
		var item = this.P.DOMInspector[id];
		if (item.node.isComponent == true)
		{
			return;
		}
		this._ExecEvent('onInsert');
	},
	InsertNode:function(result)
	{
		/*
		result:
			isTag
				Tag
			ComponentName
		*/
		var node = this.P.OldSelect;
		if (!node) {
			return;
		}
		var id = node.GetId();
		var item = this.P.DOMInspector[id];
		if (item.node.isComponent == true)
		{
			return;
		}
		if (result.isTag == true) {
			var newNode = jsForms.DesignMode.AddTag(item.node,result.Tag);
		}
		else
		{
			var newNode = jsForms.DesignMode.AddComponent(item.node,result.ComponentName);
		}
		if (newNode != undefined)
		{
			var tree = this.C.tvComponentDOM;
			var nodeTV =tree.CreateNode(this.P.DOMInspector.length);
			this.P.DOMInspector.push({node:newNode,p:id,nodeTV:nodeTV});
			nodeTV.SetText(jsForms.DesignMode.GenerateNodeName(newNode));
			item.nodeTV.AddChild(nodeTV);
		}
	},
	UpdateNode:function()
	{
		var node = this.P.OldSelect;
		if (!node) {
			return;
		}
		var id = node.GetId();
		var item = this.P.DOMInspector[id];
		item.nodeTV.SetText(jsForms.DesignMode.GenerateNodeName(item.node));
		this.UpdateTreeViewMenu();
	},
	Delete_Element: function(){
		var node = this.P.OldSelect;
		if (!node) {
			return;
		}
		if (!confirm('Do you want to delete this component?'))
		{
			return;
		}
		var id = node.GetId();
		var item = this.P.DOMInspector[id];

		if (jsForms.DesignMode.DeleteDOMElement(item.node) == true) {
			var tv = this.C.tvComponentDOM;
			var pNode = tv.GetParent(node);
			if (pNode) {
				pNode.RemoveChild(node);
			}
			this.C.TreeViewMenu.hide();
			delete (this.P.DOMInspector[id]);
			this._ExecEvent('onDelete', item);
		}
	}
};

jsForms.CreateCompileInfoByObject(
{
	Name: 'DOMConfig',
	Properties: {
	},
	Events: {
		onSelect: {},
		onMove:{},
		onDelete:{},
		onInsert:{}
	}
});
